package com.guxingke.gblog.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.guxingke.gblog.dmo.Category;
import com.guxingke.gblog.dto.ArticleLiteDto;
import com.guxingke.gblog.service.ArticleService;
import com.guxingke.gblog.service.CategoryService;
import com.guxingke.gblog.service.impl.ArticleServiceImpl;
import com.guxingke.gblog.service.impl.CategoryServiceImpl;
import com.guxingke.gblog.util.StringUtil;
import com.opensymphony.xwork2.ActionSupport;

public class CategoryAction extends ActionSupport implements
		ServletRequestAware {
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;

	private CategoryService categoryService = new CategoryServiceImpl();
	private ArticleService articleService = new ArticleServiceImpl();

	private String categoryId;
	private String mainPage;
	Category category;
	List<ArticleLiteDto> articles;

	@Override
	public String execute() throws Exception {
		request.setAttribute("mainPage", "template/category/list.jsp");
		return SUCCESS;
	}

	public String detail() throws Exception {
		categoryId = request.getParameter("categoryId");
		if (StringUtil.isEmpty(categoryId)) {
			return ERROR;
		}
		category = categoryService.getCategory(Integer.parseInt(categoryId));
		articles = articleService.getArticlesByCategory(Integer
				.parseInt(categoryId));
		mainPage = "template/category/detail.jsp";
		return SUCCESS;
	}

	public String getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}

	public String getMainPage() {
		return mainPage;
	}

	public void setMainPage(String mainPage) {
		this.mainPage = mainPage;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public List<ArticleLiteDto> getArticles() {
		return articles;
	}

	public void setArticles(List<ArticleLiteDto> articles) {
		this.articles = articles;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
}
